package com.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.auth.common.result.Result;
import com.auth.model.system.SysRole;
import com.auth.service.SysRoleService;
import com.auth.service.SysUserRoleService;
import com.auth.vo.system.AssginRoleVo;
import com.auth.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:Tangjiachang
 * @Date 2023/7/12
 * @Description:
 */
@Api(tags ="角色管理接口")
@RestController
@RequestMapping("/admin/system/role")
public class SysRoleController {
    private final SysRoleService sysRoleService;
    private final SysUserRoleService sysUserRoleService;
    public SysRoleController(SysRoleService sysRoleService,
                             SysUserRoleService sysUserRoleService) {
        this.sysRoleService = sysRoleService;
        this.sysUserRoleService = sysUserRoleService;
    }

    @ApiOperation("根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssing(@PathVariable Long userId) {
        return Result.ok(sysRoleService.findRoleByUserId(userId));
    }

    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        return Result.ok(sysRoleService.doAsking(assginRoleVo));
    }


    @GetMapping("/findAll")
    @ApiOperation("查询所有角色")
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleService.list());
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                               @PathVariable Long limit,
                                                SysRoleQueryVo sysRoleQueryVo) {
        String roleName = sysRoleQueryVo.getRoleName();
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(roleName)) {
            queryWrapper.like(SysRole::getRoleName, roleName);
        }
        Page<SysRole> rolePage = sysRoleService.page(new Page<>(page, limit), queryWrapper);
        return Result.ok(rolePage);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        return Result.ok(sysRoleService.save(sysRole));
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
       return Result.ok(sysRoleService.getById(id));
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        return Result.ok(sysRoleService.updateById(sysRole));
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        return Result.ok(sysRoleService.removeById(id));
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        return Result.ok(sysRoleService.removeBatchByIds(ids));
    }

}
